package com.demo2do.jit.injection.module;

import com.demo2do.jit.remote.api.JitApi;

import java.util.concurrent.TimeUnit;

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * @author wilson
 */
@Module
public class RemoteModule {

    @Provides
    @Singleton
    JitApi provideAuthApi(Retrofit retrofit) {
        return retrofit.create(JitApi.class);
    }

    @Provides
    @Singleton
    Retrofit provideAppRetrofit(OkHttpClient.Builder okHttpBuilder, @Named("app_server") String baseUrl, HttpLoggingInterceptor httpLoggingInterceptor) {

        OkHttpClient okHttpClient = okHttpBuilder.addInterceptor(httpLoggingInterceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();


        return new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(baseUrl)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    @Provides
    @Singleton
    OkHttpClient.Builder provideOkHttpClientBuilder() {
        return new OkHttpClient.Builder();
    }

    @Provides
    @Singleton
    HttpLoggingInterceptor provideHttpLoggingIntercepter() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        return logging;
    }

}
